package test_helper

import (
	"encoding/json"
	httpexpect "github.com/gavv/httpexpect/v2"
	"github.com/mjiulee/lego/utils"
	. "saas_sys_base/testcase/testdata"
)
func PostTestUrl(t httpexpect.LoggerReporter, path string, urlParam interface{}, req interface{}, headers map[string]string) (resp *httpexpect.Response){
	e := httpexpect.New(t, TestDataConf.TestUrl) //创建一个httpexpect实例
	resp = e.POST(path).WithQueryObject(urlParam).WithJSON(req).WithHeaders(headers).Expect()
	respbody := resp.Body().Raw()

	//错误时，打印请求内容及返回内容
	t.Logf("req.body:%s\n", utils.GetPrettyJson(req))
	t.Logf("resp.JSON:%s\n", respbody)

	return resp
}

func PostTestUrl2(t httpexpect.LoggerReporter, path string, urlParam interface{}, req interface{}, headers map[string]string, respObj interface{}) (resp *httpexpect.Response, returnRespObj interface{}){
    e := httpexpect.New(t, TestDataConf.TestUrl) //创建一个httpexpect实例
    resp = e.POST(path).WithQueryObject(urlParam).WithJSON(req).WithHeaders(headers).Expect()
    respbody := resp.Body().Raw()

    //错误时，打印请求内容及返回内容
    t.Logf("req.body:%s\n", utils.GetPrettyJson(req))
    t.Logf("resp.JSON:%s\n", respbody)

	if err := json.Unmarshal([]byte(respbody), respObj); err != nil {
		return resp, nil
	}

    return resp, respObj
}